<?php

namespace app\store\controller\order;

use app\store\controller\Controller;
use app\store\model\Order as OrderModel;
use app\store\model\OrderRefund as OrderRefundModel;
use app\store\model\ReturnAddress as ReturnAddressModel;
use think\db;

/**
 * 售后管理
 * Class Refund
 * @package app\store\controller\order
 */
class Refund extends Controller
{
    /**
     * 帮助中心列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new OrderRefundModel;
        $list = $model->getList($this->getData());
        //用户等级
        $user_grade_data = Db::table('yoshop_user_grade')->where('status', '=', '1')
            ->where('is_delete', '=', '0')->column('grade_id,name');
        return $this->fetch('index', compact('list','user_grade_data'));
    }


    /**
     * 售后单详情
     * @param $order_refund_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_refund_id)
    {
        // 售后单详情
        $detail = OrderRefundModel::detail($order_refund_id);

        // 订单详情
        $order = OrderModel::detail($detail['order_id']);
        //用户等级
        $user_grade_data = Db::table('yoshop_user_grade')->where('status', '=', '1')
            ->where('is_delete', '=', '0')->column('grade_id,name');
        // 退货地址
        $address = (new ReturnAddressModel)->getAll();
        return $this->fetch('detail', compact('detail', 'order', 'address','user_grade_data'));
    }

    /**
     * 商家审核
     * @param $order_refund_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function audit($order_refund_id)
    {

        if (!$this->request->isAjax()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        
        // 测试
        // return $model->audit($this->postData('refund'));
        
        if ($model->audit($this->postData('refund'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 确认收货并退款
     * @param $order_refund_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function receipt($order_refund_id)
    {
        if (!$this->request->isAjax()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->receipt($this->postData('refund'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改备注
     */
    public function updateRemark(){

        $data = input('post.');
        if(!$data['refund_remark']){
            return $this->renderError('请填写备注');
        }
        $status = Db::table('yoshop_order_refund')->where('order_refund_id',$data['order_refund_id'])->update(
            [
                'refund_remark'=>$data['refund_remark']
            ]
        );
        if($status){
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError('修改失败');
    }

}
